import { getMongoRepository, } from "typeorm";

import { Teachers } from '../models/Teacher'

class AdminService {
  // 分页查询（废弃）
  find(skipNum: number, limit: number) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.find({
      skip: skipNum,
      take: limit
    })
  }

  // 查询全部数据
  findAll() {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.find()
  }

  // 根据账号查询 teachers 用户
  findOneByAccount(account: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.findOne({ account })
  }

  // 根据 id 查询 teacher 用户
  findById(id: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.findOne(id)
  }

  // 根据 编号 查询 teacher 用户
  findByIdentify(identify: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.findOne({ identify })
  }


  // 创建  teacher 用户
  async save(teacher: Teachers) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.save(teacher)
  }

  // 查询 teachers 用户总数量
  async teacherCount() {
    const teacherRepository = getMongoRepository(Teachers);
    const [users, count] = await teacherRepository.findAndCount()
    return count
  }

  // 更新头像
  updateAvatar(id: string, avatar: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.update(id, { avatar })
  }

  // 根据 编号 查询 teacher 用户
  findByPhone(phone: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.findOne({ phone })
  }

  // 修改密码
  updatePassWord(id: string, password: string) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.update(id, { password })
  }

  // 修改用户状态
  updateState(id: string, state: boolean) {
    const adminRepository = getMongoRepository(Teachers);
    return adminRepository.update(id, { state })
  }

  // 修改信息
  updateInfo(id: string, teacher: Teachers) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.update(id, teacher)
  }

  // 根据名字模糊查询
  findByNameKey(name: string, skipNum: number, limit: number) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.find({
      where: {
        name: { $regex: name }
      },
      skip: skipNum,
      take: limit
    })
  }
  // 根据名字模糊查询，总数量
  async findByNameKeyCount(name: string) {
    const teacherRepository = getMongoRepository(Teachers);
    const [users, count] = await teacherRepository.findAndCount({
      where: {
        name: { $regex: name }
      }
    })
    return count
  }

  // 根据id进行删除
  deleteById(id: string) {
    const teacherRepository = getMongoRepository(Teachers)
    return teacherRepository.delete(id)
  }

  // 创建多个用户
  saveMany(teachers: Teachers[]) {
    const teacherRepository = getMongoRepository(Teachers);
    return teacherRepository.save(teachers)
  }
}

export default new AdminService()